package com.dkm.ec.activity.domain;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.exception.PartinException;
import com.dkm.biz.gl.exception.StockDeficiencyException;
import com.dkm.ec.activity.persistence.dao.LimitBuyActivityPartinDao;
import com.dkm.ec.activity.persistence.dao.LimitBuyActivitySkuDao;
import com.dkm.ec.activity.persistence.entity.LimitBuyActivityPartinPo;
import com.dkm.ec.activity.persistence.entity.LimitBuyActivitySkuPo;
import com.dkm.ec.activity.repository.LimitBuyActivityPartinRepository;
import com.dkm.ec.stock.domain.Stock;
import com.dkm.ec.stock.persistence.dao.StockQueryDao;
import com.dkm.ec.stock.persistence.entity.StockPo;
import com.dkm.ec.stock.repository.StockRepository;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by fanjinyuan on 2017/8/11.
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class LimitBuyActivityPartin extends AbstractDomain<String, LimitBuyActivityPartinPo> {

    private LimitBuyActivityPartinDao limitBuyActivityPartinDao;

    @Resource
    private StockQueryDao stockQueryDao;
    @Resource
    private StockRepository stockRepository;
    @Resource
    private LimitBuyActivitySkuDao limitBuyActivitySkuDao;
    @Resource
    private LimitBuyActivityPartinRepository limitBuyActivityPartinRepository;

    protected void init(){
        limitBuyActivityPartinDao = SpringHelper.getBean(LimitBuyActivityPartinDao.class);
        setDao(limitBuyActivityPartinDao);
    }

    public void partin(LimitBuyActivityPartinPo po) throws StockDeficiencyException {
        // 判断商家是否已参加该活动
        Integer count = limitBuyActivityPartinRepository.countForShopId(po.getActivityid(), po.getShopid());
        if(count >= 1) {
            throw new PartinException("同一个商家不能参加同一次活动");
        }
        // 合并相同sku
        List<LimitBuyActivitySkuPo> skuPoList = mergeSku(po.getSkuPoList());
        // 判断库存是否不足
        for(LimitBuyActivitySkuPo skuPo : skuPoList) {
            StockPo stockPo = stockQueryDao.get(skuPo.getStockid());
            int stock = stockPo.getQty() - stockPo.getQtyReserved();
            if(skuPo.getPartinnum()>stock)
                throw new StockDeficiencyException(String.format("sku: %s 库存不足", stockPo.getSkuId()));
            // 减少总库存数
            int dis = stockPo.getQty() - skuPo.getPartinnum();
            stockPo.setQty(dis);
            Stock stockIns = stockRepository.newInstance(stockPo);
            stockIns.update();
        }

        //写入参与记录
        po.setPartintime(new Date());
        this.setData(po);
        this.create();
        // 写入sku
        for(LimitBuyActivitySkuPo skuPo : skuPoList) {
            skuPo.setPartinid(po.getId());
            skuPo.setAvailableNum(skuPo.getPartinnum());
            skuPo.setIsReturnStock("0");
            limitBuyActivitySkuDao.create(skuPo);
        }
    }

    public List<LimitBuyActivitySkuPo> mergeSku(List<LimitBuyActivitySkuPo> skuPoList) {
        Map<String, LimitBuyActivitySkuPo> map = new HashMap<String, LimitBuyActivitySkuPo>();
        for (LimitBuyActivitySkuPo po : skuPoList) {
            String stockId = po.getStockid();
            if (map.containsKey(stockId)) {
                Integer partiNnum = map.get(stockId).getPartinnum();
                partiNnum += po.getPartinnum();
                map.get(stockId).setPartinnum(partiNnum);
            } else {
                map.put(stockId, po);
            }
        }
        List<LimitBuyActivitySkuPo> list = new ArrayList<LimitBuyActivitySkuPo>();
        Iterator<LimitBuyActivitySkuPo> iterator = map.values().iterator();
        while(iterator.hasNext()) {
            LimitBuyActivitySkuPo next = iterator.next();
            list.add(next);
        }
        return list;
    }

}
